package net.wangds.auth.grant.service;

import net.wangds.auth.grant.comp.GeneralGrantComp;
import net.wangds.auth.grant.entity.GeneralGrantEntity;
import net.wangds.auth.module.*;
import net.wangds.jpa.service.AbstractJpaService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.List;

@Service
public class GrantService extends AbstractJpaService<GeneralGrantEntity, GeneralGrantComp> {

    @Autowired
    private GeneralGrantComp mmCmpGrant;

    @Override
    protected GeneralGrantComp fetchJpaComp() {
        return mmCmpGrant;
    }

    public List<GeneralGrantEntity> listByOwnerId(String accountId){
        return mmCmpGrant.listByOwnerId(accountId);
    }

    public List<GeneralGrantEntity> listGrantedByOwnerId(String accountId){
        return mmCmpGrant.listByOwnerIdAndAction(accountId, 15);
    }

    public void grant(Resource res, User user, int privilege) {
        mmCmpGrant.grant(res, user, privilege);
    }

    public void grant(Resource res, Role role, int privilege) {
        mmCmpGrant.grant(res, role, privilege);
    }

    public void grant(Resource res, Job job, int privilege) {
        mmCmpGrant.grant(res, job, privilege);
    }

    public void grant(Resource res, Account account, int privilege) {
        mmCmpGrant.grant(res, account, privilege);
    }

    public void ungrant(Resource res, User user, int privilege) {
        mmCmpGrant.ungrant(res, user, privilege);
    }

    public void ungrant(Resource res, Role role, int privilege) {
        mmCmpGrant.ungrant(res, role, privilege);
    }

    public void ungrant(Resource res, Job job, int privilege) {
        mmCmpGrant.ungrant(res, job, privilege);
    }

    public void ungrant(Resource res, Account account, int privilege) {
        mmCmpGrant.ungrant(res, account, privilege);
    }

    public List<GeneralGrantEntity> listByOwnerIdAndSchemaAndAction(String id, ResourceSchemaEnum menu, int action) {
        return mmCmpGrant.findByOwnerIdAndSchemaAndAction(id, menu, action);
    }
}
